﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TruyenCuoi.ViewModel;
using System.Windows.Media;
using Microsoft.Phone.Tasks;

namespace TruyenCuoi
{
    public partial class DetailStory : PhoneApplicationPage
    {
        private DetailStoryViewModel viewModel;
        public DetailStory()
        {
            InitializeComponent();
            viewModel = new DetailStoryViewModel();
            DataContext = viewModel;
            var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            var imageBrush = new ImageBrush();
            var imageUri = new Uri(isLightTheme == Visibility.Visible ? "/Assets/480x800.jpg" : "/Assets/BackGroundPage.png", UriKind.Relative);
            imageBrush.ImageSource = new System.Windows.Media.Imaging.BitmapImage(imageUri);
            LayoutRoot.Background = imageBrush;
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarMenuItem_Click1(object sender, EventArgs e)
        {
            facebook.Visibility = Visibility.Visible;
            facebook.btnOK.Click += btnOK_Click;
            facebook.btnCancel.Click += btnCancel_Click;
            facebook.LinkVideos.Content = "Share Content";
            facebook.LinkPage.Content = "Share Image";
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            facebook.Visibility = Visibility.Collapsed;
        }

        void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string Share = string.Empty;
            string title = "Truyện cười: " + DetailStoryViewModel.Detail.StoryName;
            facebook.btnOK.Click -= btnOK_Click;
            
            if (facebook.LinkPage.IsChecked == true)
            {
                Share ="Nội dung câu chuyện: " + DetailStoryViewModel.Detail.StoryContent;
            }

            facebook.Visibility = Visibility.Collapsed;
            NavigationService.Navigate(new Uri("/FacebookLoginPage.xaml?subject=" + title+"&body="+ Share, UriKind.Relative));
        }

        private void play_Click(object sender, RoutedEventArgs e)
        {
            var player = new MediaPlayerLauncher
            {
                Media = new Uri(viewModel.UrlSong, UriKind.Absolute),
                Controls = MediaPlaybackControls.All,
                Location = MediaLocationType.Data,
                Orientation = MediaPlayerOrientation.Landscape
            };
            player.Show();
        }
    }
}